-
Notifications
You must be signed in to change notification settings - Fork 50
Add User-Agent
header to all outbound requests coming from the API
#877
Conversation
API Developer Docs Preview: Ready https://wordpress.github.io/openverse-api/_preview/877 Please note that GitHub pages takes a little time to deploy newly pushed code, if the links above don't work or you see old versions, wait 5 minutes and try again. You can check the GitHub pages deployment action list to see the current status of the deployments. |
docker-compose.yml
Outdated
@@ -18,7 +18,7 @@ services: | |||
environment: | |||
PORT: 8222 | |||
MALLOC_ARENA_MAX: 2 | |||
command: ["-enable-url-source"] | |||
command: ["-enable-url-source -forward-headers User-Agent"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This new option will need to be ported up to the infrastructure repository as well, if it is merged.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is a reasonable configuration for now. The new env var you've added OUTBOUND_USER_AGENT_TEMPLATE
should also be added to the env.template
file (if only as a comment because the default value is good enough).
I agree that in the future we can use a central solution to manage requests but that will require all HTTP requests to first use the same libraries which will take a significant amount of time.
a1a3e53
to
2ac52a4
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is great, thank you for adding thorough tests too. Agreed that a more centralized implementation would be cool but the variety of request methods we have precludes that. I think the "purpose" interpolation you've added is excellent 💯
I have a few comments around the tests but the operational code is ✔️ IMO
|
||
assert len(grequests.requests) > 0 | ||
for r in grequests.requests: | ||
assert HEADERS == r.kwargs["headers"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: All the other tests use the format assert actual == expected
whereas this one seems reversed.
|
||
|
||
@pytest.fixture(autouse=True) | ||
def requests(monkeypatch) -> RequestsFixture: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a thought, I notice that some of these fixtures differ slightly but a few of them (this one and the same fixture in watermark_test.py
). Do you think it'd be possible to move some of these up to a common conftest.py
so they only need to be defined once? Particularly with fixtures as simple as api_client
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would rather do this refactor in another PR as part of another issue that more careful looks at the state of our unit test fixtures in general and thoughtfully establishes a pattern. For now most of these have subtle differences that would be tedious to abstract.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good 🙂
758f033
to
2695333
Compare
2ac52a4
to
bad1118
Compare
Blocked by #875 |
bad1118
to
e5b4c8b
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
New changes look good 👍🏼
Fixes
Fixes #826 by @sarayourfriend
Description
The fix should be applied to all outbound requests that could make a request to Wikimedia, not just in the Watermark endpoint. This is all of our outbound requests, so I've had to update all our outbound requests for it.
I tried to explore some ways of centralising this configuration, primarily by subclasses
requests.Session
and forcing apurpose
kwarg to be present on all calls to::request
that would then configure the UA header, but it seemed heavy and I wasn't sure exactly how it would work or if folks would like that. I think it would make things a little easier to manage, or at least easier to not make mistakes with. But we also usegrequests
andurlopen
which would still need their own bespoke solution... so I opted for the simplest approach across the board for now. We can improve this/generalise/abstract on it later if we want.Testing Instructions
Run the app locally and ensure there are no issues.
Check out the unit tests. The ones involving urlopen were a complete nightmare to write!
Checklist
Update index.md
).main
) or a parent feature branch.Developer Certificate of Origin
Developer Certificate of Origin